//
//  ViewController.m
//  UI通讯录
//
//  Created by ZF on 16/5/28.
//  Copyright © 2016年 ZF. All rights reserved.
//

#import "ViewController.h"
#import "Linkman.h"
#import "TwoViewController.h"

@interface ViewController ()
{
    NSArray *data;
    NSInteger rows;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.tabBarItem.title = @"通讯录";
    self.navigationItem.title = @"联系人";
    self.view.backgroundColor = [UIColor grayColor];
    
    self.refreshControl = [UIRefreshControl new];
    self.refreshControl.tintColor = [UIColor blackColor];
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Loading" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(onClick)];
   // self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"5"]];
    
    
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *url = [bundle URLForResource:@"00001" withExtension:@"vcf"];
   
    NSString *vcf = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSArray *arr = [vcf componentsSeparatedByString:@"\n"];
    
    NSMutableArray *mutableArr = [NSMutableArray new];
    Linkman *linkman;
    for(NSString *str in arr)
    {
        if([str hasPrefix:@"BEGIN"])
        {
            linkman = [Linkman new];
        }
        else if([str hasPrefix:@"END"])
        {
            [mutableArr addObject:linkman];
        }
        else if([str hasPrefix:@"FN"])
        {
            NSArray *array = [str componentsSeparatedByString:@":"];
            linkman.name = array[1];
        }
        else if([str hasPrefix:@"TEL"])
        {
            NSArray *array = [str componentsSeparatedByString:@":"];
            NSString *string = [array[1] stringByReplacingOccurrencesOfString:@"-" withString:@""];
            linkman.telphone = string;
        }
    }
    data = mutableArr;
}


- (void)onClick
{
    [self.refreshControl endRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    
    Linkman *linkman = data[indexPath.row];
    cell.textLabel.text = linkman.name;
    
    cell.imageView.image = [UIImage imageNamed:@"5"];
    cell.imageView.layer.cornerRadius = 18;//cornerRadius 半径
   // cell.imageView.clipsToBounds = YES;
    
    cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TwoViewController *view = [TwoViewController new];
    Linkman *man = data[indexPath.row];
    view.man = man;

    [self.navigationController pushViewController:view animated:YES];
}


- (NSInteger)initWithRow
{
    return rows;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
